package com.packt.webstore.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.packt.webstore.domain.Product;
import com.packt.webstore.domain.repository.ProductRepository;
import com.packt.webstore.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private ProductRepository productRepository;


	public void processOrder(String productId, int quantity) {
Product productById = productRepository.getProductById(productId);
		
		if(productById.getUnidadeDeStock() < quantity){
			throw new IllegalArgumentException("Out of Stock. Available Units in stock " +
		productById.getUnidadeDeStock());
		}
		
		productById.setUnidadeDeStock(productById.getUnidadeDeStock() - quantity);		
	}

}
